/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inter;

import Logic.Conectividad;
import Logic.MessageBox;
import Logic.This;
import java.util.Vector;

/**
 *
 * @author ronaldo
 */
public class CtrProveedor extends javax.swing.JInternalFrame {

    /**
     * @Var Internas -Modus :0-Ingreso :1-Modificacion
     */
    private int Modus = 0;

    /**
     * Creates new form CtrProveedor
     */
    public CtrProveedor() {
        initComponents();
        LoadData();
        Clean();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtTelefono = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtCorreo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtUbicacion = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        lblSave = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TblProveedor = new javax.swing.JTable();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Control de Proveedores");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos del Proveedor", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century", 0, 12))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N
        jLabel1.setText("Codigo");

        txtCodigo.setEditable(false);
        txtCodigo.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N
        jLabel2.setText("Nombre");

        txtNombre.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N

        txtTelefono.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N
        jLabel3.setText("Telefono");

        txtCorreo.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N
        jLabel4.setText("Correo");

        txtUbicacion.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N
        jLabel5.setText("Ubicacion");

        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile.png"))); // NOI18N
        lblSave.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        lblSave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblSaveMouseClick1(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblSaveMouseEnter1(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblSaveMouseExit1(evt);
            }
        });

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/edit-clear.png"))); // NOI18N
        jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel6MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNombre)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(txtCorreo, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
                    .addComponent(txtUbicacion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblSave)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(txtCorreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtUbicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(4, 4, 4))
                            .addComponent(lblSave))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Detalle de Proveedores", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century", 0, 12))); // NOI18N

        TblProveedor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Telefono", "Correo", "Ubicacion"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TblProveedor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TblProveedorMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TblProveedor);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LoadData() {
        javax.swing.table.DefaultTableModel Model = (javax.swing.table.DefaultTableModel) TblProveedor.getModel();
        Model.setRowCount(0); //Limpiando Tabla
        try {
            String Sql = "SELECT * FROM PROVEEDOR";
            java.sql.ResultSet result = Conectividad.Conector.createStatement().executeQuery(Sql);
            while (result.next()) {
                Vector v = new Vector();
                v.add(result.getString(1));
                v.add(result.getString(2));
                v.add(result.getString(3));
                v.add(result.getString(4));
                v.add(result.getString(5));
                Model.addRow(v);
            }
            TblProveedor.setModel(Model);
        } catch (java.sql.SQLException e) {
            MessageBox.show(this, e.getMessage(), "Se ha generado un Error, al ingresar los datos\n" + e.getMessage(), MessageBox.Error);
        } catch (Exception e) {
            MessageBox.show(this, e.getMessage(), "Se ha presentado un error en el sistema", MessageBox.Error);
        }
    }

    private void Clean() {
        txtCodigo.setText(This.AlfaNum(10));
        This.Clean(txtNombre);
        This.Clean(txtTelefono);
        This.Clean(txtCorreo);
        This.Clean(txtUbicacion);
        Modus = 0;
    }

    private void Save() {
        String Codigo, Nombre, Telefono, Correo, Ubicacion;
        Codigo = txtCodigo.getText();
        Nombre = txtNombre.getText();
        Telefono = txtTelefono.getText();
        Correo = txtCorreo.getText();
        Ubicacion = txtUbicacion.getText();
        String Sql = "";
        if (Modus == 0) {
            Sql = "INSERT INTO PROVEEDOR VALUES('" + Codigo + "','" + Nombre + "','" + Telefono + "','" + Correo + "','" + Ubicacion + "')";
        } else if (Modus == 1) {
            Sql = "     UPDATE PROVEEDOR"
                    + " SET NOMBRE='" + Nombre + "',TELEFONO='" + Telefono + "',CORREO='" + Correo + "',UBIACION='" + Ubicacion + "'"
                    + " WHERE COD_PROV='" + Codigo + "'";
        }
        try {
            Conectividad.Conector.createStatement().executeUpdate(Sql);
            MessageBox.show(this, "Ejecuto correctamente!", "Se ha ejecutado correctamente la instruccion", MessageBox.Informacion);
        } catch (java.sql.SQLException e) {
            MessageBox.show(this, e.getMessage(), "Se ha presentado un error\n" + e.getMessage(), MessageBox.Error);
        }
        Modus = 0;
        Clean();
        LoadData();
    }

    private void lblSaveMouseClick1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseClick1
        // TODO add your handling code here:
        Save();
    }//GEN-LAST:event_lblSaveMouseClick1

    private void lblSaveMouseEnter1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseEnter1
        // TODO add your handling code here:
        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile2.png")));
    }//GEN-LAST:event_lblSaveMouseEnter1

    private void lblSaveMouseExit1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseExit1
        // TODO add your handling code here:
        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile.png")));
    }//GEN-LAST:event_lblSaveMouseExit1

    private void jLabel6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel6MouseClicked
        // TODO add your handling code here:
        Clean();
    }//GEN-LAST:event_jLabel6MouseClicked

    private void TblProveedorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TblProveedorMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            int Sel = TblProveedor.getSelectedRow();
            txtCodigo.setText(TblProveedor.getValueAt(Sel, 0).toString());
            txtNombre.setText(TblProveedor.getValueAt(Sel, 1).toString());
            txtTelefono.setText(TblProveedor.getValueAt(Sel, 2).toString());
            txtCorreo.setText(TblProveedor.getValueAt(Sel, 3).toString());
            txtUbicacion.setText(TblProveedor.getValueAt(Sel, 4).toString());
            Modus = 1;
        }
    }//GEN-LAST:event_TblProveedorMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable TblProveedor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblSave;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtCorreo;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtTelefono;
    private javax.swing.JTextField txtUbicacion;
    // End of variables declaration//GEN-END:variables
}
